<?php
/**
 * Created by PhpStorm.
 * User: cen
 * Date: 2020/6/26
 * Time: 17:52
 */

namespace app\index\controller;


use think\Controller;
use app\index\model\MessageModel;
use think\facade\Session;

class Message extends Controller
{
    public function index(){

        $list = MessageModel::with('user')->order('create_time desc')->paginate(10);
        $this->assign('list', $list);
        return $this->fetch();
    }


    public function submit(){
        $data = $this->request->param();
        //$sid = $this->request->cookie('indexSid');
        //return json($sid);
        //$user = Session::get($sid);

        //if($user == null){
            //return json('请登录后再留言',401);
        //}
        if(empty( $data['id'])){
            return json('请登录后再留言',401);
        }

        $pidName = '';
        if($data['pid'] != 0){
            $res = MessageModel::with('user')->find($data['pid']);
            $pidName = $res['user']['username'];
        }

        MessageModel::create([
            'user_id' => $data['id'],
            'content' => $data['value'],
            'pid' => $data['pid'],
            'pid_name' => $pidName,
        ]);

        return json('ok');

    }

}